/****************************************************************************
*
* Copyright(c) 2023  C*Core -   All Rights Reserved
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS, " AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* PROJECT     : CCFC2011BC
* DESCRIPTION : LowPower Demo Code
* HISTORY     : Initial version.
* @file     lowpower_demo.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/
#include "lowpower_demo.h"
#include "led_hw.h"
#include "console.h"
#include "sys.h"
#include "Mcu_lld.h"

/*******************************************************************************
 * @brief      WakeUp_PIN_Config
 * @param[in]  None
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void WakeUp_PIN_Config(void) /* PRQA S 1505 */
{
    SIU.PCR[41].R = 0x0103;    /*PC9->GND, Wakeup Chip*/
}

/*******************************************************************************
 * @brief     LowPower_Standby
 * @param[in]  None
 * @param[out] None
 * @retval
 * @notapi
 *******************************************************************************/
void LowPower_Standby(void)
{
    vuint32_t i_index = 3;

    while (i_index-- != 0U) /* PRQA S 3387, 3440 */
    {
        delay_ms(500);
    }

    ME.RUN[0].R   = 0x001F0010u;
    ME.PCTL[68].R = (uint8_t)PCTL_SEL_RUN_PC1;   /* SIUL control */
    ME.PCTL[69].R = (uint8_t)PCTL_SEL_RUN_PC1;   /* WKPU control */

    WakeUp_PIN_Config();
   // (void)PSPRINTF("\n LowPower_Standby Case Start!\n");
   // (void)PSPRINTF("\n Into standby mode, Please give a falling edge to PC9 for reset!\n");

    WKPU_Config((uint32_t)ME_MODE_STANDBY);
}

/*******************************************************************************
 * @brief     LowPower_Stop
 * @param[in]  None
 * @param[out] None
 * @retval
 * @notapi
 *******************************************************************************/
void LowPower_Stop(void)
{
    vuint32_t i_index = 3;

    while (i_index-- != 0U) /* PRQA S 3387, 3440 */
    {
        delay_ms(500);
    }

    ME.RUN[0].R   = 0x001F0010u;
    ME.PCTL[68].R = ((uint8_t)PCTL_SEL_RUN_PC1 | (uint8_t)PCTL_SEL_LP_PC1);   /* SIUL control */
    ME.PCTL[69].R = ((uint8_t)PCTL_SEL_RUN_PC1 | (uint8_t)PCTL_SEL_LP_PC1);   /* WKPU control */

    WakeUp_PIN_Config();

    (void)PSPRINTF("\n LowPower_Stop Case Start!\n");
    (void)PSPRINTF("\n Into stop mode, Please give a falling edge to PC9 for continue!\n");

    WKPU_Config((uint32_t)ME_MODE_STOP);
}
